package iterator_pattern;

/**
 * 具体迭代器对象（反向迭代）
 * @author ricejson
 */
public class ConcreteIteratorDesc implements Iterator {

    private ConcreteAggregate aggregate;
    private int current;

    public ConcreteIteratorDesc(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
        this.current = aggregate.count() - 1;
    }

    @Override
    public Object first() {
        return aggregate.get(aggregate.count() - 1);
    }

    @Override
    public Object next() {
        current--;
        if (current >= 0) {
            return aggregate.get(current);
        }
        return null;
    }

    @Override
    public boolean isDone() {
        return current < 0;
    }

    @Override
    public Object currentItem() {
        return aggregate.get(current);
    }
}
